################################################################################
# TARGET CONFIGURATION
################################################################################

# As supported MCUs expand, the target cores can expand to suit.
DEFAULT_TARGET = CortexM0P
TARGETS = CortexM0P CortexM4F CortexM4NF CortexM7

ifndef TARGET
$(warning No target has been defined.  Default target $(DEFAULT_TARGET) is being used.)
TARGET := $(DEFAULT_TARGET)
endif
ifeq (,$(findstring $(TARGET), $(TARGETS)))
$(info The named target '$(TARGET)' is not a valid target.)
$(error Legal targets are: $(TARGETS))
endif

ifdef MAKEDBG
$(info TARGET is '$(TARGET)'.)
endif

################################################################################
# MAKE LIBRARY CONFIGURATION
################################################################################

include $(MAKE_ROOT)/mk_lib.mk
TARGET_LOWER := $(call lowercase,$(TARGET))
TARGET_UPPER := $(call uppercase,$(TARGET))
ifdef MAKEDBG
$(info TARGET_LOWER is '$(TARGET_LOWER)'.)
$(info TARGET_UPPER is '$(TARGET_UPPER)'.)
endif

################################################################################
# ARCHITECTURE CONFIGURATION
################################################################################

include $(MAKE_ROOT)/$(TARGET).mk
ifdef MAKEDBG
$(info TOOLING_PREFIX is '$(TOOLING_PREFIX)'.)
$(info IARCH is '$(IARCH)'.)
$(info ISUBSET is '$(ISUBSET)'.)
$(info TARGET_TYPE is '$(TARGET_TYPE)'.)
$(info TARGET_FPU is '$(TARGET_FPU)'.)
$(info TARGET_FAPI is '$(TARGET_FAPI)'.)
endif

################################################################################
# SOURCE PATH AND FILE CONFIGURATION
################################################################################

COROS_SOURCE_PATH  := ./src
COROS_SOURCE_FILES := $(wildcard $(COROS_SOURCE_PATH)/*.c)
COROS_SOURCE_IMPL_PATH := $(COROS_SOURCE_PATH)/$(ISUBSET)
COROS_SOURCE_IMPL_FILES := $(wildcard $(COROS_SOURCE_IMPL_PATH)/co_impl_$(IARCH)_$(ISUBSET)_gcc.c)
ifdef MAKEDBG
$(info COROS_SOURCE_PATH is '$(COROS_SOURCE_PATH)'.)
$(info COROS_SOURCE_FILES is '$(COROS_SOURCE_FILES)'.)
$(info COROS_SOURCE_IMPL_PATH is '$(COROS_SOURCE_IMPL_PATH)'.)
$(info COROS_SOURCE_IMPL_FILES is '$(COROS_SOURCE_IMPL_FILES)'.)
endif

COROS_INCLUDE_PATH  := ./include
COROS_INCLUDE_FILES := $(wildcard $(COROS_INCLUDE_PATH)/*.h)
ifdef MAKEDBG
$(info COROS_INCLUDE_PATH is '$(COROS_INCLUDE_PATH)'.)
$(info COROS_INCLUDE_FILES is '$(COROS_INCLUDE_FILES)'.)
endif

################################################################################
# COMPILER TARGET CONFIGURATION
################################################################################

BUILD_ROOT                := ./build
TARGET_ROOT               := $(BUILD_ROOT)/$(TARGET)
TARGET_OBJECT_FILES       := $(addprefix $(TARGET_ROOT)/, $(notdir $(addsuffix .o, $(basename $(COROS_SOURCE_FILES)))))
TARGET_OBJECT_IMPL_FILES  := $(addprefix $(TARGET_ROOT)/, $(notdir $(addsuffix .o, $(basename $(COROS_SOURCE_IMPL_FILES)))))
TARGET_ALL_OBJECT_FILES   := $(TARGET_OBJECT_FILES) $(TARGET_OBJECT_IMPL_FILES)
ifdef MAKEDBG
$(info BUILD_ROOT is '$(BUILD_ROOT)'.)
$(info TARGET_ROOT is '$(TARGET_ROOT)'.)
$(info TARGET_OBJECT_FILES is '$(TARGET_OBJECT_FILES)'.)
$(info TARGET_OBJECT_IMPL_FILES is '$(TARGET_OBJECT_IMPL_FILES)'.)
$(info TARGET_ALL_OBJECT_FILES is '$(TARGET_ALL_OBJECT_FILES)'.)
endif

################################################################################
# LIBRARY CONFIGURATION
################################################################################

LIB_ROOT                  := ./lib
TARGET_LIB                := $(LIB_ROOT)/libcoros_$(TARGET_UPPER).a
ifdef MAKEDBG
$(info LIB_ROOT is '$(LIB_ROOT)'.)
$(info TARGET_LIB is '$(TARGET_LIB)'.)
endif

################################################################################
# PACKAGING CONFIGURATION
################################################################################

PACKAGE_ROOT              := ./packages
TARGET_PACKAGE_TAR        := $(PACKAGE_ROOT)/coros_$(TARGET_LOWER).tar
TARGET_PACKAGE_COMPRESSED := $(TARGET_PACKAGE_TAR).xz
ifdef MAKEDBG
$(info PACKAGE_ROOT is '$(PACKAGE_ROOT)'.)
$(info TARGET_PACKAGE_TAR is '$(TARGET_PACKAGE_TAR)'.)
$(info TARGET_PACKAGE_COMPRESSED is '$(TARGET_PACKAGE_COMPRESSED)'.)
endif

################################################################################
# TOOL CONFIGURATION
################################################################################

AR      := $(TOOLING_PREFIX)ar
ARFLAGS := rsv
ifdef MAKEDBG
$(info AR is '$(AR)'.)
$(info ARFLAGS is '$(ARFLAGS)'.)
endif

CC     := $(TOOLING_PREFIX)gcc
CFLAGS := -g -Os -Werror -Wall $(TARGET_TYPE) $(TARGET_FAPI) $(TARGET_FPU)
CFLAGS += -D $(TARGET) $(addprefix -I , $(COROS_INCLUDE_PATH)) -MMD -MP
CFLAGS += $(BOARD_INCLUDE_PATH) -L $(LIB_ROOT)
ifdef MAKEDBG
$(info CC is '$(CC)'.)
$(info CFLAGS is '$(CFLAGS)'.)
endif

TAR      := tar
TARFLAGS := --verbose --file=$(TARGET_PACKAGE_TAR)
ifdef MAKEDBG
$(info TAR is '$(TAR)'.)
$(info TARFLAGS is '$(TARFLAGS)'.)
endif

XZ      := xz
XZFLAGS := --compress --force -9 --extreme --verbose
ifdef MAKEDBG
$(info XZ is '$(XZ)'.)
$(info XZFLAGS is '$(XZFLAGS)'.)
endif

#$(error Ending here...)
